#include "mainwindow.h"
#include "ui_mainwindow.h"

#define DEBUG

MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
    : QMainWindow(parent, flags), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

#ifdef Q_WS_QWS
    QScreenCursor * cursor = QScreenCursor::instance();
    QImage alpha(1,1,QImage::Format_ARGB32);
    alpha.load(QString(QApplication::applicationDirPath() + "/images/cursor.png"));
    cursor->set(alpha,0,0);
#endif
    QFontDatabase db;
    QStringList list = db.families(QFontDatabase::Japanese);

#ifdef DEBUG
    qDebug() << "=====FONT FAMILIES DETECTED=====";
    foreach(QString families, list){
        qDebug() << families;
    }
#endif

    init();

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init()
{
        int x,y,w,h;

        this->scene = new QGraphicsScene();

        settings = new QSettings("tsmpc","tsmpc");

        //load audio backend
        settings->setValue("audiobackend","amarok");
        QString audio_backend = settings->value("audiobackend").toString();

        QPluginLoader loader("libtsmpc_" + audio_backend + "backend.so");
        QObject * plug = loader.instance();
        if(plug != 0){
            qDebug() << "plugin loading successful";
            audioplayer =  qobject_cast<AudioPlayerBackend *>(plug);
            qDebug() << audioplayer->features();
            qDebug() << audioplayer->identifier();
            //qDebug() << "plugin name: " << interface->name();
        }else{
            qDebug() << "plugin loading failed";
            qDebug() << loader.errorString();
        }

        //load theme settings
        settings->setValue("theme","blue");
        QString theme = settings->value("theme").toString();
        themeSettings = new QSettings(QDir::currentPath() + "/themes/" + theme + "/" + theme + ".theme",QSettings::IniFormat);

        background = new QPixmap(QString(QApplication::applicationDirPath() + "/themes/" + theme + themeSettings->value("backgroundImage").toString()));
        //qDebug() << QString(QApplication::applicationDirPath() + "/themes/" + theme + themeSettings->value("backgroundImage").toString());
        this->scene->setBackgroundBrush(QBrush(*background));

        x = themeSettings->value("sceneRectx").toInt();
        y = themeSettings->value("sceneRecty").toInt();
        w = themeSettings->value("sceneRectw").toInt();
        h = themeSettings->value("sceneRecth").toInt();
        this->scene->setSceneRect(x,y,w,h);
        this->setGeometry(x,y,w,h);
        //qDebug() << w;

        aview = new audioview(audioplayer, scene, themeSettings);
        connect(aview, SIGNAL(showBrowserButtonClicked()), this, SLOT(showBrowser()));

        collection = new Collection(audioplayer);
        model = new CollectionItemModel(collection);
        browser = new collectionBrowser(model);

        connect(model, SIGNAL(songSelected(Song*)), audioplayer, SLOT(play(Song*)));
        connect(model, SIGNAL(songSelected(Song*)), this, SLOT(hideBrowser()));
        connect(model, SIGNAL(atRoot()), this, SLOT(hideBrowser()));

        ui->graphicsView->setScene(scene);

        //libtsmpc_cover.so.1.0.0
        /*QPluginLoader loader("libtsmpc_cover.so");
        QObject * plug = loader.instance();
        if(plug != 0){
            qDebug() << "plugin loading successful";
            WidgetInterface * interface =  qobject_cast<WidgetInterface *>(plug);
            qDebug() << "plugin name: " << interface->name();
        }else{
            qDebug() << "plugin loading failed";
            qDebug() << loader.errorString();
        }*/
}

void MainWindow::showBrowser()
{
    #ifdef Q_WS_QWS
        browser->showFullScreen();
    #else
        browser->show();
    #endif
}

void MainWindow::hideBrowser()
{
    browser->hide();
}
